package com.lqk.shardingjdbcproj.controller;

import com.lqk.shardingjdbcproj.entity.Course;
import com.lqk.shardingjdbcproj.entity.response.ReturnReponse;
import com.lqk.shardingjdbcproj.enums.ResponseEnum;
import com.lqk.shardingjdbcproj.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 406 Not Acceptable
 * 异常HttpMediaTypeNotAcceptableException解决方案:
 *
 *  1，在controller返回的对象中必须包含getter/setter方法；
 *  2，在controller的@RequestMapping(value= "/findWxmpType", method = RequestMethod.POST,produces="application/json")
 *      加上红色字体部分。并没有解决问题，produces这个属性，大家可以百度下其具体作用是什么。简单来说：就是用于匹配页面的请求头Accept信息是否和响应头Accept信息（produces设置）一致。
 *  3，配置jackson转换器，指定返回数据类型为text/html;
 */
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping(value="/addCourse",method = RequestMethod.POST)
    public ReturnReponse addCourse(@RequestBody Course course){
//        Course course = new Course();
//        course.setCid(5L);
//        course.setCname("MYSQL#");
//        course.setUserId(300L);
//        course.setCstatus("Normal");
        int i = courseService.insertCourse(course);
        if (i>0){
            return ReturnReponse.success(ResponseEnum.CODE_200.getCode(), ResponseEnum.CODE_200.getMessage());
        }else {
            return ReturnReponse.fail(ResponseEnum.CODE_400.getCode(), ResponseEnum.CODE_400.getMessage());
        }
    }

    @RequestMapping(value="/getCourse",method = RequestMethod.POST)
    public ReturnReponse getCourse(@RequestParam long cid){
        Course course = courseService.selectOne(cid);
        return ReturnReponse.success(ResponseEnum.CODE_200.getCode(), ResponseEnum.CODE_200.getMessage(),course);
    }

    // 水平分库
    @RequestMapping(value="/addCourseDb",method = RequestMethod.POST)
    public ReturnReponse addCourseDb(@RequestBody Course course){
        int i = courseService.insertCourse(course);
        if (i>0){
            return ReturnReponse.success(ResponseEnum.CODE_200.getCode(), ResponseEnum.CODE_200.getMessage());
        }else {
            return ReturnReponse.fail(ResponseEnum.CODE_400.getCode(), ResponseEnum.CODE_400.getMessage());
        }
    }

    @RequestMapping(value="/getCourseDb",method = RequestMethod.POST)
    public ReturnReponse getCourseDb(@RequestParam long cid){
        Course course = courseService.selectOne(cid);
        return ReturnReponse.success(ResponseEnum.CODE_200.getCode(), ResponseEnum.CODE_200.getMessage(),course);
    }

}
